package greentea.webloginmodule.jaas;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

/**
 * The application implements the CallbackHandler.
 * 
 * <p>
 * This application is text-based. Therefore it displays information to the user
 * using the OutputStreams System.out and System.err, and gathers input from the
 * user using the InputStream System.in.
 */
class MyCallbackHandler implements CallbackHandler {

	private String userLogin = null;
	private String userPassword = null;
	
	public MyCallbackHandler (String userLogin, String userPassword) {
		this.userLogin = userLogin;
		this.userPassword = userPassword;
	}
	/**
	 * Invoke an array of Callbacks.
	 * 
	 * <p>
	 * 
	 * @param callbacks
	 *            an array of <code>Callback</code> objects which contain the
	 *            information requested by an underlying security service to be
	 *            retrieved or displayed.
	 * 
	 * @exception java.io.IOException
	 *                if an input or output error occurs.
	 *                <p>
	 * 
	 * @exception UnsupportedCallbackException
	 *                if the implementation of this method does not support one
	 *                or more of the Callbacks specified in the
	 *                <code>callbacks</code> parameter.
	 */
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {

		for (int i = 0; i < callbacks.length; i++) {
			if (callbacks[i] instanceof TextOutputCallback) {
				// display the message according to the specified type
				TextOutputCallback toc = (TextOutputCallback) callbacks[i];
				switch (toc.getMessageType()) {
				case TextOutputCallback.INFORMATION:
					System.out.println(toc.getMessage());
					break;
				case TextOutputCallback.ERROR:
					System.out.println("ERROR: " + toc.getMessage());
					break;
				case TextOutputCallback.WARNING:
					System.out.println("WARNING: " + toc.getMessage());
					break;
				default:
					throw new IOException("Unsupported message type: "
							+ toc.getMessageType());
				}
			} else if (callbacks[i] instanceof NameCallback) {
				// prompt the user for a username
				NameCallback nc = (NameCallback) callbacks[i];
				nc.setName(this.userLogin);
			} else if (callbacks[i] instanceof PasswordCallback) {
				// prompt the user for sensitive information
				PasswordCallback pc = (PasswordCallback) callbacks[i];
				pc.setPassword(this.userPassword.toCharArray());
			} else {
				throw new UnsupportedCallbackException(callbacks[i],
						"Unrecognized Callback");
			}
		}
	}

}